package com.ficus.road.maintain.strategy.allfactory;

import com.ficus.road.maintain.core.model.*;
import com.ficus.road.maintain.core.model.zh.OverrunCarZH;
import com.ficus.road.maintain.dao.HitAllFactoryDao;
import com.ficus.road.maintain.util.MyDateUtil;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.ficus.road.maintain.core.model.HitStatusEnum.NEW;
import static com.ficus.road.maintain.core.model.HitStatusEnum.NOTED;

/**
 * @author xiawen yang
 * @date 2022/2/15 下午5:45
 */
public class OverrunCarStrategy implements AllFactoryStrategy<OverrunCarZH> {

    private static String type = AllFactoryTypeEnum.OVERRUN_CAR.getType();

    private HitAllFactoryDao hitAllFactoryDao;

    public OverrunCarStrategy(HitAllFactoryDao hitAllFactoryDao) {
        this.hitAllFactoryDao = hitAllFactoryDao;
    }

    @Override
    public Hit convert2AllFactoryHit(OverrunCarZH overrunCarZH) {
        Hit fetchHit = hitAllFactoryDao.fetch(overrunCarZH.getUnid());
        String findTimeStr = overrunCarZH.getFindtime();
        Date findTime = MyDateUtil.strParseDate(findTimeStr, "yyyy-MM-dd'T'HH:mm:ss");
        GpsLocation gpsLocation = GpsLocation.builder().latitude(31.235870d).longitude(121.621148d).build();
        String[] coordinates = overrunCarZH.getXy().split(",");
        if (ObjectUtils.isNotEmpty(coordinates) && coordinates.length == 2) {
            gpsLocation = GpsLocation.builder()
                    .longitude(Double.parseDouble(coordinates[0].trim()))
                    .latitude(Double.parseDouble(coordinates[1].trim())).build();
        }
        if (ObjectUtils.isEmpty(fetchHit)) {
            Hit hit = new Hit();
            HitAttrs hitAttrs = new HitAttrs();
            hitAttrs.setConfirmation(Confirmation.builder().build());
            hit.setScene("CITY");
            hit.setAttrs(hitAttrs);
            hit.setCreationTime(new Date().getTime() / 1000);
            hit.setFirstHitTime(findTime.getTime() / 1000);
            hit.setLastModificationTime(new Date().getTime() / 1000);
            hitAttrs.setRoadName(overrunCarZH.getAddress());
            hitAttrs.getConfirmation().setStatus(Confirmation.ConfirmationStatus.CORRECT);
            hitAttrs.setFrameImageUrl(overrunCarZH.getFindpic());
            hitAttrs.setSceneImage(overrunCarZH.getFindpic());
//            hitAttrs.setLimitHour(null);
            hitAttrs.setGpsLocation(gpsLocation);
            hit.setId(overrunCarZH.getUnid());
            hit.setType(type);
            hit.setNeedPushOut(false);
            // 超限超载只有发现事件，没有后续的处置流程
            List<HistoryHit> historyHits = new ArrayList<>();
            HistoryHit historyHit = new HistoryHit();
            historyHits.add(historyHit);
            HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
            historyHit.setAttrs(historyHitAttrs);
            historyHit.setHitTime(hit.getFirstHitTime());
            historyHitAttrs.setStatus(NEW);
            historyHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());
            // 提醒件状态变更为已提醒
            HistoryHit notedHistoryHit = new HistoryHit();
            historyHits.add(0,notedHistoryHit);
            HistoryHitAttrs notedHistoryHitAttrs = new HistoryHitAttrs();
            notedHistoryHit.setAttrs(notedHistoryHitAttrs);
            notedHistoryHit.setHitTime(hit.getFirstHitTime());
            notedHistoryHitAttrs.setStatus(NOTED);
            notedHistoryHitAttrs.setSceneImage(hit.getAttrs().getSceneImage());

            hit.getAttrs().setStatus(NOTED);

            hit.setList(historyHits);
            return hit;
        } else {
//            fetchHit.setLastModificationTime(new Date().getTime() / 1000);
//            List<HistoryHit> list = fetchHit.getList();
//            HistoryHit historyHit = new HistoryHit();
//            historyHit.setHitTime(findTime.getTime() / 1000);
//            HistoryHitAttrs historyHitAttrs = new HistoryHitAttrs();
//            historyHitAttrs.setSceneImage(fetchHit.getAttrs().getSceneImage());
//            historyHit.setAttrs(historyHitAttrs);
            fetchHit.getAttrs().setGpsLocation(gpsLocation);
            return fetchHit;
        }
    }
}
